import {Inform, User} from '../db/model.js'

export const message = (socket, io) => {

    socket.on('send1', (d) => {
        console.log(`收到${d}`)
        socket.emit('send1',d)
    })

    socket.on('add_user', async (data) => {
        try {
            const inform = await Inform.create(data);
            socket.to('room1').emit('add_users', {sender: inform.sender._id, receiver: inform.receiver._id});
        } catch (e) {
            console.error('出错:', e);
        }
    });

    socket.on('agree_add_user', async (inform) => {
        try {
            console.log(inform)
            await Inform.updateOne({_id: inform._id}, {agree: true})
            socket.to('room1').emit('agree_add_user', {sender: inform.sender._id, receiver: inform.receiver._id})
        } catch (e) {
            console.log(e)
        }
    })
    socket.on('un_agree_add_user', async (inform) => {
        try {
            console.log(inform)
            await Inform.updateOne({_id: inform._id}, {agree: false, status: 'un_agree'})
            socket.to('room1').emit('un_agree_add_user', {sender: inform.sender._id, receiver: inform.receiver._id})
        } catch (e) {
            console.log(e)
        }
    })
}